package com.puff.controller.platform;

import com.puff.dto.*;
import com.puff.entity.ShopCategory;
import com.puff.result.PageResult;
import com.puff.result.Result;
import com.puff.service.ShopCategoryService;
import com.puff.vo.ShopCategoryVO;
import com.puff.vo.ShopVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/platform/shopCategory")
@Slf4j
@Api(tags = "商家分类管理相关接口")
public class ShopCategoryController {

    @Autowired
    private ShopCategoryService shopCategoryService;

    /**
     * 新增商家分类
     * @param shopCategoryDTO
     * @return
     */
    @PostMapping
    @ApiOperation("新增商家分类")
    public Result save(@RequestBody ShopCategoryDTO shopCategoryDTO){
        log.info("新增商家分类: {}",shopCategoryDTO);
        shopCategoryService.saveShopCategory(shopCategoryDTO);

        // 清理缓存数据
        //String key = "dish_" + dishDTO.getCategoryId();
        //cleanCache(key);
        return Result.success();
    }

    /**
     * 商家分类分页查询
     * @param shopCategoryPageQueryDTO
     * @return
     */
    @GetMapping("/page")
    @ApiOperation("商家分类分页查询")
    public Result<PageResult> page(ShopCategoryPageQueryDTO shopCategoryPageQueryDTO){
        log.info("商家分类分页查询:{}",shopCategoryPageQueryDTO);
        PageResult pageResult = shopCategoryService.pageQuery(shopCategoryPageQueryDTO);
        return Result.success(pageResult);
    }

    /**
     * 批量删除商家分类
     * @param ids
     * @return
     */
    @DeleteMapping
    @ApiOperation("批量删除商家分类")
    public Result delete(@RequestParam List<Long> ids){
        // @RequestParam注解能够让mybatis去解析前端发送过来的字符串，并添加到ids中
        log.info("批量删除商家分类:{}",ids);
        shopCategoryService.deleteBatch(ids);

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

    /**
     * 查询所有商家分类
     * @param
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("查询所有商家分类")
    public Result<List<ShopCategory>> list(){
        log.info("查询所有商家分类......");
        List<ShopCategory> list = shopCategoryService.list();
        return Result.success(list);
    }

    /**
     * 根据id查询商家分类信息
     * @param id
     * @return
     */
    @GetMapping("/{id}")
    @ApiOperation("根据id查询商家分类信息")
    public Result<ShopCategoryVO> getById(@PathVariable Long id){
        log.info("根据id查询商家分类信息:{}",id);
        ShopCategoryVO shopCategoryVO = shopCategoryService.getShopCategoryById(id);
        return Result.success(shopCategoryVO);
    }

    /**
     * 修改商家分类信息
     * @param shopCategoryDTO
     * @return
     */
    @PutMapping
    @ApiOperation("修改商家分类信息")
    public Result update(@RequestBody ShopCategoryDTO shopCategoryDTO){
        log.info("修改商家分类信息:{}",shopCategoryDTO);
        shopCategoryService.updateShopCategory(shopCategoryDTO);

        //TODO 缓存处理

        // 将所有菜品缓存数据都清理掉，所有以dish_开头的key
        //cleanCache("dish_*");

        return Result.success();
    }

}
